<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="res/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("#table1 #tr3 #p1start").click(function(){
		$.ajax({
			"url":"board.do",
			"type":"post",
			"data":"method=p1StopYes",
			"dataType":"json",
			"success":function(){
			}
		});
	});
	$("#table1 #tr3 #p1start").click(function(){
		
		$.ajax({
			"url":"board.do",
			"type":"post",
			"data":"method=selectP1Status",
			"dataType":"json",
			"success":function(returnData){
					if(returnData==0){
						p1status1();
					}else{
						alert("딴데서 실행중임");
					}
			}
		});
	});
	
	$("#table2 #tr5 #p2start").click(function(){
		$.ajax({
			"url":"board.do",
			"type":"post",
			"data":"method=p2",
			"dataType":"json",
			"success":function(returnData){
					var div = document.getElementById("div");
					var div2 = document.createElement("div");
					if(returnData!=null){
					div2.innerHTML=returnData.p1+"p2다 신난다";
					}else{
						div.innerHTML="p2널임";
					}
					div.appendChild(div2);
					p2();
			}
		});
	});
	$("#table #tr #start").click(function(){
		$.ajax({
			"url":"board.do",
			"type":"post",
			"data":"method=startstopYes",
			"dataType":"json",
			"success":function(){
			}
		});
	});
	$("#table #tr #start").click(function(){
		$.ajax({
			"url":"board.do",
			"type":"post",
			"data":"method=start",
			"dataType":"json",
			"success":function(returnData){
					var div = document.getElementById("div");
					var div2 = document.createElement("div");
					div2.innerHTML=returnData.a;
					div.appendChild(div2);
					if(returnData.stop==0){
					start();
					}else{
						
					}
			}
		});
	});
	$("#table #tr2 #stop").click(function(){
		$.ajax({
			"url":"board.do",
			"type":"post",
			"data":"method=startstopNo",
			"dataType":"json",
			"success":function(){
			}
		});
	});
	$("#table1 #tr4 #p1stop").click(function(){
		$.ajax({
			"url":"board.do",
			"type":"post",
			"data":"method=p1StopNo",
			"dataType":"json",
			"success":function(){
			}
		});
	});	
});

function p1start(){
	$.ajax({
		"url":"board.do",
		"type":"post",
		"data":"method=p1",
		"dataType":"json",
		"success":function(returnData){
				var div = document.getElementById("div");
				var div2 = document.createElement("div");
				if(returnData.b!=null){
				div2.innerHTML=returnData.b.generate+"야 신난다";
				}else{
					div2.innerHTML="p1널임";
				}
				div.appendChild(div2);
				if(returnData.stop==0){
				p1();
				}else{
					
				}
		}
	});
}
function p1status1(){
	$.ajax({
		"url":"board.do",
		"type":"post",
		"data":"method=updateP1Status1",
		"dataType":"json",
		"success":function(){
			p1start();
		}
	});
}

function p1(){
	$.ajax({
		"url":"board.do",
		"type":"post",
		"data":"method=p1",
		"dataType":"json",
		"success":function(returnData){
				var div = document.getElementById("div");
				var div2 = document.createElement("div");
				if(returnData.b!=null){
				div2.innerHTML=returnData.b.generate+"야 신난다";
				}else{
					div2.innerHTML="p1널임";
				}
				div.appendChild(div2);
				if(returnData.stop==0){
				p1();
				}else{
					
				}
		}
	});
}

function p2(){
	$.ajax({
		"url":"board.do",
		"type":"post",
		"data":"method=p2",
		"dataType":"json",
		"success":function(returnData){
				var div = document.getElementById("div");
				var div2 = document.createElement("div");
				if(returnData!=null){
				div2.innerHTML=returnData.p1+"p2다 신난다";
				}else{
					div2.innerHTML="p2널임";
				}
				div.appendChild(div2);
				p2();
		}
	});
}

function start(){
	$.ajax({
		"url":"board.do",
		"type":"post",
		"data":"method=start",
		"dataType":"json",
		"success":function(returnData){
				var div = document.getElementById("div");
				var div2 = document.createElement("div");
				div2.innerHTML=returnData.a;
				div.appendChild(div2);
				if(returnData.stop==0){
				start();
				}else{
					
				}
		}
	});
}
$(window).unload(function(){
	$.ajax({
		"url":"board.do",
		"type":"post",
		"data":"method=p1StopNo",
		"dataType":"json",
		"success":function(){
		}
	});
});





</script>
<title>Insert title here</title>
</head>
<body>
<table id="table">
<tr id="tr">
<td id="start">제너레이터 시작</td>
</tr>
<tr id="tr2">
<td id="stop">제너레이터 그만</td>
</tr>
</table>
<table id="table1">
<tr id="tr3">
<td id="p1start">p1 시작</td>
</tr>
<tr id="tr4">
<td id="p1stop">p1 그만</td>
</tr>
</table>
<table id="table2">
<tr id="tr5">
<td id="p2start">p2 시작</td>
</tr>
<tr id="tr6">
<td id="p2stop">p2 그만</td>
</tr>
</table>
<div id="div"></div>
</body>
</html>